package com.jhgsys.internal.base.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.jhgsys.internal.base.entity.BaseSafetyScoreConfig;
import com.jhgsys.internal.base.mapper.BaseSafetyScoreConfigMapper;
import com.jhgsys.internal.base.service.IBaseSafetyScoreConfigService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 安全积分设置 Service实现
 *
 * @author admin
 * @date 2024-08-07 11:34:15
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class BaseSafetyScoreConfigServiceImpl extends ServiceImpl<BaseSafetyScoreConfigMapper, BaseSafetyScoreConfig> implements IBaseSafetyScoreConfigService {

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-08-07 11:34:15
     * @param request QueryRequest
     * @param baseSafetyScoreConfig baseSafetyScoreConfig
     * @return IPage<BaseSafetyScoreConfig>
    */
    @Override
    public IPage<BaseSafetyScoreConfig> findBaseSafetyScoreConfigs(QueryRequest request, BaseSafetyScoreConfig baseSafetyScoreConfig) {
        LambdaQueryWrapper<BaseSafetyScoreConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseSafetyScoreConfig::getDeletedFlg, 0);
        Page<BaseSafetyScoreConfig> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author admin
     * @date 2024-08-07 11:34:15
     * @param baseSafetyScoreConfig baseSafetyScoreConfig
     * @return List<BaseSafetyScoreConfig>
     */
    @Override
    public List<BaseSafetyScoreConfig> findBaseSafetyScoreConfigList(BaseSafetyScoreConfig baseSafetyScoreConfig) {
	    LambdaQueryWrapper<BaseSafetyScoreConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseSafetyScoreConfig::getDeletedFlg, 0);
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author admin
    * @date 2024-08-07 11:34:15
    * @param id
    * @return BaseSafetyScoreConfig
    */
    @Override
    public BaseSafetyScoreConfig findBaseSafetyScoreConfigById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author admin
     * @date 2024-08-07 11:34:15
     * @param baseSafetyScoreConfig baseSafetyScoreConfig
     */
    @Override
    @Transactional
    public void createBaseSafetyScoreConfig(BaseSafetyScoreConfig baseSafetyScoreConfig) {
        baseSafetyScoreConfig.setCreatedTime(new Date());
        this.save(baseSafetyScoreConfig);
    }

    /**
     * 修改
     * @author admin
     * @date 2024-08-07 11:34:15
     * @param baseSafetyScoreConfig baseSafetyScoreConfig
     */
    @Override
    @Transactional
    public void updateBaseSafetyScoreConfig(BaseSafetyScoreConfig baseSafetyScoreConfig) {
        baseSafetyScoreConfig.setUpdatedTime(new Date());
        this.saveOrUpdate(baseSafetyScoreConfig);
    }

    /**
     * 逻辑删除
     * @author admin
     * @date 2024-08-07 11:34:15
     * @param baseSafetyScoreConfig baseSafetyScoreConfig
     * @param baseSafetyScoreConfigIds baseSafetyScoreConfigIds
     */
    @Override
    @Transactional
    public void deleteBaseSafetyScoreConfig(BaseSafetyScoreConfig baseSafetyScoreConfig,String baseSafetyScoreConfigIds) {
        List<String> list = Arrays.asList(baseSafetyScoreConfigIds.split(StringPool.COMMA));
        this.baseMapper.update(baseSafetyScoreConfig,new LambdaQueryWrapper<BaseSafetyScoreConfig>().in(BaseSafetyScoreConfig::getSafetyScoreConfigId,list));
	}

    /**
    * 物理删除
    * @author admin
    * @date 2024-08-07 11:34:15
    * @param baseSafetyScoreConfigIds baseSafetyScoreConfigIds
    */
    @Override
    @Transactional
    public void physicallyDeleteBaseSafetyScoreConfig(String baseSafetyScoreConfigIds) {
        List<String> list = Arrays.asList(baseSafetyScoreConfigIds.split(StringPool.COMMA));
        LambdaQueryWrapper<BaseSafetyScoreConfig> wapper = new LambdaQueryWrapper<>();
        wapper.in(BaseSafetyScoreConfig::getSafetyScoreConfigId,list);
        this.remove(wapper);
    }

    /**
     * 根据所属公司查询
     * @param tenantId
     * @return
     */
    @Override
    public BaseSafetyScoreConfig findBaseSafetyScoreConfigByTenantId(String tenantId){
        LambdaQueryWrapper<BaseSafetyScoreConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseSafetyScoreConfig::getDeletedFlg, 0);
        queryWrapper.eq(BaseSafetyScoreConfig::getTenantId, tenantId);
        queryWrapper.last(" LIMIT 1 ");
        return this.baseMapper.selectOne(queryWrapper);
    }
}
